capture	log close
log using MAR03-MedModel,  replace text

//  program:    Stata 
//  task:       Sample and diagnostics 
//  project:    Markets  

version
clear all
macro drop _all
set linesize 80
set more off
local tag " 07-05-23| Cleaned 06-06-24"
local file "MAR03-MedModel"
local note "|`tag' | `file'"
local opt "noparen sideway excel noaster  bdec(2)  sdec(2)  pdec(3)   adec(2) e(r2) stats(coef se pval)"
local dv "lrhom"
local iv "fraser"
local iv2 "infantmort" 
local cont " edu unemp  popdense perurban  sexratio"
local cont2 " gini edu unemp  popdense perurban  sexratio"
local icid " i1 i2 i3 i4 i5 i6 i7 i8 i9 i10 i11 i12 i13 i14 i15 i16 i17 i18 i19 i20 i21 i22 i23 i24 i25 i26 i27 i28 i29 i30 i31 i32 i33 i34 i35 i36 i37 i38 i39 i40 i41 i42 i43 i44 i45 i46 i47 i48 i49 i50 i51 i52 i53 i54 i55 i56 i57 i58 i59 i60 i61 i62 i63 i64 i65 i66 i67 i68 i69 i70 i71 i72 i73 i74 i75 i76 i77 i78 i79 i80 i81 i82 i83 i84 i85  i87 i88"
local iyear " x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 x11 x12 x13 x14 x15 x16 x17 x18 x19 "

//	#0
//	Loading data 
use MAR02-SampDiag.dta, clear 

//	#1
//	Xtset up 
xtset CID year 

//	#2
//	mediation model set up (nation fixed effects)
tab CID, gen(i)

//	#3
//	mediator model set up (time fixed effects) 
tab year, gen(x) 

//	#4
//	Mediation Model `iv' through infant mort to `dv' nation fixed effects
sem (`iv'-> `iv2' , ) (`iv' `iv2' -> `dv', ) (`cont' -> `dv', ) (`icid' ->`dv')  ,  vce(cluster CID) nocapslatent
outreg2 using `file', replace `opt'  addnote(`iv'->`iv2')

//	#5
//	Mediation Model Frasher through infant mort to `dv' nation & year fixed effects 
sem ( `iv'-> `iv2', ) (`iv' `iv2' -> `dv', ) (`cont' -> `dv', ) (`icid' `iyear' ->`dv')  ,  vce(cluster CID) nocapslatent
outreg2 using `file', append `opt'  addnote(`iv'->`iv2' nationXyear)

//	#6
//	Mediation model Infant mort -> Frasher to `dv' nation fixed effects
sem ( `iv2' -> `iv', ) (`iv' `iv2' -> `dv', ) (`cont' -> `dv', ) (`icid' ->`dv')  ,  vce(cluster CID) nocapslatent
outreg2 using `file', append `opt'  addnote(`iv2'->`iv')

//	#7
//	Mediation model Infant mort -> Faser to `dv' nation and year fixed effects
sem ( `iv2'-> `iv', ) (`iv' `iv2' -> `dv', ) (`cont' -> `dv', ) (`icid' `iyear' ->`dv')  ,  vce(cluster CID) nocapslatent
outreg2 using `file', append `opt'  addnote(`iv2'->`iv' nationXyear)

//	#8
//	Mediation model for hertiage -> infant 
sem ( heritage-> `iv2', ) (heritage `iv2' -> `dv', ) (`cont' -> `dv', ) (`icid' ->`dv')  ,  vce(cluster CID) nocapslatent
outreg2 using `file', append `opt'  addnote(heritage->`iv2')

/*
//	#10
//	Mediation model for heritage -> infant nation and year fixed effects
sem ( heritage-> `iv2', ) (heritage `iv2' -> `dv', ) (`cont' -> `dv', ) (`icid' `iyear'->`dv')  ,  vce(cluster CID) nocapslatent
outreg2 using `file', append `opt' 
*/

//	#11
//	Mediation models for rol => infant 
sem ( rol-> `iv2', ) (rol `iv2' -> `dv', ) (`cont' -> `dv', ) (`icid' ->`dv')  ,  vce(cluster CID) nocapslatent
outreg2 using `file', append `opt'  addnote(rol->`iv2')

//	#12
//	Mediation models for  infnat=> rol 
sem ( `iv2'-> rol, ) ( `iv2' rol -> `dv', ) (`cont' -> `dv', ) (`icid' ->`dv')  ,  vce(cluster CID) nocapslatent
outreg2 using `file', append `opt'    addnote(`iv2'->rol)

//	#13
//	save and close 
note:  Hertiage heritage -> infant unable to find maximum | `note'.
note:  Mediation models | `note'. 
notes
save `file'.dta, replace 
log close 
//